package logic

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"video-joy/models/mysql"

	"video-joy/userrpc/internal/svc"
	"video-joy/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type VideoUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVideoUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoUpdateLogic {
	return &VideoUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 视频修改
func (l *VideoUpdateLogic) VideoUpdate(in *userrpc.VideoUpdateRequest) (*userrpc.VideoUpdateResponse, error) {
	// todo: add your logic here and delete this line

	video := &mysql.VideoInfo{
		Model:         gorm.Model{ID: uint(in.UserId)},
		UserId:        in.UserId,
		VideoTitle:    in.VideoTitle,
		VideoCategory: in.VideoCategory,
		VideoLink:     in.VideoLink,
		CoverLink:     in.CoverLink,
		VideoIntro:    in.VideoIntro,
		PlayNum:       int(in.PlayNum),
		LikeNum:       int(in.LikeNum),
		CollectNum:    int(in.CollectNum),
		VideoStatus:   int(in.VideoStatus),
		TopicId:       int(in.TopicId),
	}

	if err := video.VideoUpdate(mysql.DB); err != nil {
		return nil, errors.New("视频修改失败")
	}

	return &userrpc.VideoUpdateResponse{
		VideoId: int64(video.ID),
	}, nil
}
